package lianxi2;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<Account>();

    public void start() {
        /** 启动ATM */
        while (true) {
            System.out.println("Welcome to ATM!");
            System.out.println("1. Create account");
            System.out.println("2. Log in");
            System.out.println("请选择：");
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    createAccount();
                    break;
                case 2:
                    logIn();
                    break;
                default:
                    System.out.println("Invalid input!");
                    break;
            }
        }
    }

    private void createAccount() {
        /** 创建账户 */
        Account account = new Account();

        System.out.println("请输入您的姓名：");

        Scanner sc = new Scanner(System.in);

        String name = sc.next();

        account.setName(name);
        while (true) {
            System.out.println("请输入密码：");

            String password = sc.next();

            System.out.println("请确认密码：");


            String password2 = sc.next();
            if (!password.equals(password2)) {
                System.out.println("两次输入的密码不一致，请重新输入！");
            } else {
                account.setPassword(password);
                break;
            }
        }
        while (true) {
            System.out.println("请输入您的性别:");

            String gender = sc.next();
            if (gender.equals("男") || gender.equals("女")) {
                account.setSex(gender);
                break;
            } else {
                System.out.println("性别输入错误，请重新输入！");
            }

        }
        while (true) {
            System.out.println("请输入银行卡限额:");
            double limit = sc.nextDouble();
            if (limit > 0) {
                account.setLimit(limit);
                break;
            } else {
                System.out.println("限额输入错误，请重新输入！");
            }
        }
        account.setIdcard(createIdcard());
        accounts.add(account);
        System.out.println("恭喜您，账户创建成功！");
        System.out.println("您的卡号为：" + account.getIdcard());

    }

    private void logIn() {
        /** 登录 */
        System.out.println("请输入卡号：");
        Scanner sc = new Scanner(System.in);
        String idcard = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        Account account = null;
        for (Account acc : accounts) {
            if (acc.getIdcard().equals(idcard) && acc.getPassword().equals(password)) {
                account = acc;
                break;
            }
        }
        if (account == null) {
            System.out.println("卡号或密码错误，请重新输入！");
        } else {
            System.out.println("欢迎，" + account.getName() + "！");
            while (true) {
                System.out.println("1. 取款");
                System.out.println("2. 存款");
                System.out.println("3. 转账");
                System.out.println("4. 查询余额");
                System.out.println("5. 修改密码");
                System.out.println("6. 退出");
                System.out.println("7. 注销账号");
                System.out.println("请选择：");
                int num = sc.nextInt();
                switch (num) {
                    case 1:
                        withdraw(account);
                        break;
                    case 2:
                        deposit(account);
                        break;
                    case 3:
                        transfer(account);
                        break;
                    case 4:
                        queryBalance(account);
                        break;
                    case 5:
                        modifyPassword(account);
                    case 6:
                        System.out.println("欢迎下次使用！");
                        return;
                    case 7:
                        System.out.println("是否确认注销账号？(Y/N)");
                        String confirm = sc.next();
                        if (confirm.equals("Y")) {
                            if(account.getMoney() > 0){
                                System.out.println("账号有余额，请先清空余额！");
                                break;
                            }
                            accounts.remove(account);
                            System.out.println("注销成功！");
                            return;
                        } else {
                            break;
                        }
                    default:
                        System.out.println("Invalid input!");
                        break;
                }
            }
        }

    }

    private String createIdcard() {
        /** 生成卡号 */
        String idcard = "";
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            idcard += r.nextInt(10);
        }
        Account a = findAccount(idcard);
        if (a != null) {
            return createIdcard();
        }
        return idcard;
    }

    private Account findAccount(String idcard) {

        /** 查找账户 */
        for (int i = 0; i < accounts.size(); i++) {
            Account a = accounts.get(i);
            if (a.getIdcard().equals(idcard)) {
                return a;
            }
        }
        return null;
    }

    private void withdraw(Account account) {
        /** 取款 */
        if (account.getMoney() <= 0) {
            System.out.println("余额不足，请充值！");
            return;
        }
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入取款金额：");
            double amount = sc.nextDouble();
            if (amount <= 0) {
                System.out.println("金额输入错误，请重新输入！");
                continue;
            } else if (amount > account.getLimit()) {
                System.out.println("超过账户限额，请重新输入！");
                continue;
            } else if (amount > account.getMoney()) {

                System.out.println("余额不足，请重新输入！");
                continue;
            } else {
                account.setMoney(account.getMoney() - amount);
                System.out.println("取款成功！");
                System.out.println("您的余额为：" + account.getMoney());
                return;
            }
        }
    }

    private void deposit(Account account) {
        /** 存款 */
        System.out.println("请输入存款金额：");
        Scanner sc = new Scanner(System.in);
        while (true) {
            double amount = sc.nextDouble();
            if (amount <= 0) {
                System.out.println("金额输入错误，请重新输入！");
            } else {
                account.setMoney(account.getMoney() + amount);
                System.out.println("存款成功！");
                System.out.println("您的余额为：" + account.getMoney());
                return;
            }
        }
    }

    private void queryBalance(Account account) {
        /** 查询余额 */
        System.out.println("您的余额为：" + account.getMoney());
    }

    private void modifyPassword(Account account) {
        /** 修改密码 */
        while (true) {
            System.out.println("请输入原密码：");
            Scanner sc = new Scanner(System.in);
            String oldPassword = sc.next();
            if (!account.getPassword().equals(oldPassword)) {
                System.out.println("原密码错误，请重新输入！");
                continue;
            }
            System.out.println("请输入新密码：");
            String newPassword = sc.next();
            System.out.println("请确认新密码：");
            String newPassword2 = sc.next();
            if (newPassword.equals(newPassword2)) {
                account.setPassword(newPassword);
                System.out.println("密码修改成功！");
                return;
            } else {
                System.out.println("两次输入的密码不一致，请重新输入！");
                continue;
            }
        }
    }

    private void transfer(Account a1) {
        /** 转账 */
        System.out.println("请输入转账卡号：");
        Scanner sc = new Scanner(System.in);
        String idcard = sc.next();
        Account a2 = findAccount(idcard);
        if (a2 == null) {
            System.out.println("未找到该账户!");
            return;
        } else {
            while (true) {
                System.out.println("请输入该账户姓名：");

                String name = sc.next();
                if (name.equals(a2.getName())) {
                    while (true) {
                        System.out.println("请输入转账金额：");
                        double amount = sc.nextDouble();
                        if (amount <= 0) {
                            System.out.println("金额输入错误，请重新输入！");
                            continue;
                        } else if (amount > a1.getLimit()) {
                            System.out.println("超过账户限额，请重新输入！");
                            continue;
                        } else if (amount > a1.getMoney()) {
                            System.out.println("余额不足，请重新输入！");
                            continue;
                        } else {
                            a1.setMoney(a1.getMoney() - amount);
                            a2.setMoney(a2.getMoney() + amount);
                            System.out.println("转账成功！");
                            System.out.println("您的余额为：" + a1.getMoney());

                            return;
                        }
                    }
                } else {
                    System.out.println("姓名错误，请重新输入！");
                }
            }
        }
    }
}



